local function getFileList(dirname)
	local fileList = os.tmpname()
	os.execute('/usr/bin/find ' .. dirname .. ' -name "*.lua" > ' .. fileList)

	local file = io.open(fileList, "r")
	local content = file:read("*all")

	file:close()
	os.remove(fileList)

	local list = {}
	for line in string.gmatch(content, "[^\n]+") do
		table.insert(list, line)
	end
	return list
end

local function force(file)
	pcall(function() loadfile(file)() end)
end

force("lunit.lua")
force("lunit-console.lua")

-- suit file list, if it is null, find suit files from specified directory
local list = nil -- { "../MockTest.lua" }

-- suit directory, if list is null, find suit files from here
local dirname = "../../../UnitTest/Util"

if not list then
	list = getFileList(dirname)
end

print("")
for _, file in ipairs(list) do
	print("Test suit file: " .. file)
end
print("")

local ENABLE_COVERAGE = (os.getenv("ENABLE_COVERAGE") == "true")
local ENABLE_PROFILE = (os.getenv("ENABLE_PROFILE") == "true")

require "lunit"
require "Mock"
require "log"
require "lsqlite3"

-- if lua coverage is enabled
if ENABLE_COVERAGE then 
	require "luacov" 
end

-- if lua profile is enabled
if ENABLE_PROFILE then 
	require "profiler"
	profiler.start("lua_profile.out")
end

Log.configure("./log.properties")
--Template.open("../test.db", true)
local stats = lunit.main(list)

-- if lua coverage is enabled
if ENABLE_COVERAGE then 
	luacov.on_exit() 
end

-- if lua profile is enabled
if ENABLE_PROFILE then
	profiler.stop()
end

if stats.errors > 0 or stats.failed > 0 then
	os.exit(1)
end
